package com.csy.property.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csy.property.community.domain.AjBuilding;
import com.csy.property.community.domain.AjCommunity;
import com.csy.property.community.domain.AjRoom;
import com.csy.property.community.domain.AjUnit;
import com.csy.property.community.domain.dto.AjCommunityDto;
import com.csy.property.community.domain.vo.AjRoomVo;

import com.csy.property.community.domain.vo.RoomTree;
import com.csy.property.community.mapper.AjBuildingMapper;
import com.csy.property.community.mapper.AjCommunityMapper;
import com.csy.property.community.mapper.AjRoomMapper;
import com.csy.property.community.mapper.AjUnitMapper;
import com.csy.property.community.service.AjRoomService;
import com.csy.property.community.service.AjUnitService;
import com.csy.property.entity.AjCommunityArea;
import com.csy.property.mapper.AjCommunityAreaMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


@Service
public class AjRoomServiceImpl extends ServiceImpl<AjRoomMapper, AjRoomVo> implements AjRoomService {

    @Resource
    private AjRoomMapper ajRoomMapper;

    @Resource
    private AjCommunityMapper communityMapper;
    @Resource
    private AjBuildingMapper ajBuildingMapper;

    @Resource
    private AjUnitMapper ajUnitMapper;
    @Resource
    private AjCommunityAreaMapper ajCommunityAreaMapper;
    @Resource
    private AjUnitService unitService;
    @Override
    public List<AjRoomVo> selectRoomList(AjRoomVo ajRoomVo) {
        return ajRoomMapper.selectRoomList(ajRoomVo);
    }

    /**
     * 更新房间信息
     * @param ajRoomVo
     * @return
     */
    @Override
    public int updateRoom(AjRoomVo ajRoomVo) {

        return ajRoomMapper.updateById(ajRoomVo);
    }

    /**
     * 查询所有房间下拉
     * @return
     */
    @Override
    public List<RoomTree> queryPullDownAllRoom() {
        List<AjCommunityDto> ajCommunityDtos = communityMapper.queryList(new AjCommunity());
        List<RoomTree> roomTree = new ArrayList<>();
        //遍历小区
        for (AjCommunityDto communityDto: ajCommunityDtos){
            RoomTree tree = new RoomTree();
            //封装小区
            tree.setCode(communityDto.getCommunityId());
            tree.setName(communityDto.getCommunityName());
            AjUnit unit = new AjUnit();
            unit.setCommunityId(communityDto.getCommunityId());
            //查询楼栋和单元下拉
            List<RoomTree> buidingUnitTrees = unitService.queryPullDownRoom(unit);
            for (RoomTree building:buidingUnitTrees){
                List<RoomTree> units = building.getChildren();
                for (RoomTree u: units){
                    Long unitId = u.getCode();
                    //查询房间下拉
                    List<AjRoom> rooms = ajRoomMapper.queryPullDownAllRoom(unitId);
                    List<RoomTree> roomTrees = new ArrayList<>();
                    for (AjRoom room:rooms){
                        RoomTree r = new RoomTree();
                        //封装房间信息
                        r.setCode(room.getRoomId());
                        r.setName( room.getRoomName().toString());
                        roomTrees.add(r);
                    }
                    u.setChildren(roomTrees);
                }
            }
            tree.setChildren(buidingUnitTrees);
            roomTree.add(tree);
        }

        return roomTree;
    }

    /**
     * 查询小区房间信息
     * @return
     */
    @Override
    public List<RoomTree> queryCommunityRoom() {
        List<AjCommunityDto> ajCommunityDtos = communityMapper.queryList(new AjCommunity());
        List<RoomTree> roomTree = new ArrayList<>();
        for (AjCommunityDto communityDto: ajCommunityDtos) {
            RoomTree tree = new RoomTree();
            tree.setCode(communityDto.getCommunityId());
            tree.setName(communityDto.getCommunityName());
            Long communityId = communityDto.getCommunityId();
            List<AjCommunityArea> areaList = ajCommunityAreaMapper.getAreaList(new AjCommunityArea().setCommunityId(communityId));
            ArrayList<RoomTree> areaBuildUnitTree = new ArrayList<>();
            if (areaList.size() == 0){
                //area对象
                RoomTree areaTree = new RoomTree();
                areaTree.setName("无");
                areaTree.setCode(0l);
                //building集合作为child集合
                List<RoomTree> buildingtrees = new ArrayList<>();
                List<AjBuilding> list = ajBuildingMapper.queryPullDown(communityId);
                getBuildingRoomTree(buildingtrees, list);
                areaTree.setChildren(buildingtrees);
                areaBuildUnitTree.add(areaTree);
            }else {
                for (AjCommunityArea ajCommunityArea : areaList) {
                    //area对象
                    RoomTree areaTree = new RoomTree();
                    areaTree.setName(ajCommunityArea.getCommunityArea());
                    areaTree.setCode(ajCommunityArea.getId());
                    //根据area对象areaId查building
                    AjBuilding building = new AjBuilding();
                    building.setCommunityAreaId(ajCommunityArea.getId());
                    List<AjBuilding> ajBuildings = ajBuildingMapper.selectBuildingList(building);
                    //building集合作为child集合
                    List<RoomTree> buildingtrees = new ArrayList<>();
                    getBuildingRoomTree(buildingtrees,ajBuildings);
                    areaTree.setChildren(buildingtrees);
                    areaBuildUnitTree.add(areaTree);
                }
            }
            tree.setChildren(areaBuildUnitTree);
            roomTree.add(tree);
        }
        return roomTree;
    }

    /**
     * 获取楼栋单元房间树形结构
     * @param buildingtrees
     * @param list
     */
    private void getBuildingRoomTree(List<RoomTree> buildingtrees, List<AjBuilding> list) {
        for (AjBuilding building: list){
            RoomTree buildingTree = new RoomTree();
            buildingTree.setCode(building.getBuildingId());
            buildingTree.setName(building.getBuildingName());
            //unit集合
            List<RoomTree> unitTree = new ArrayList<>();
            AjUnit unit = new AjUnit();
            unit.setBuildingId(building.getBuildingId());
            //根据查到的楼栋查单元
            List<AjUnit> ajUnits = ajUnitMapper.selectUnitList(unit);
            for (AjUnit u:ajUnits){
                RoomTree ajunit = new RoomTree();
                ajunit.setCode(u.getUnitId());
                ajunit.setName(u.getUnitName());
                Long unitId = u.getUnitId();
                List<AjRoom> rooms = ajRoomMapper.queryPullDownAllRoom(unitId);

                List<RoomTree> roomTrees = new ArrayList<>();
                for (AjRoom room:rooms){
                    RoomTree r = new RoomTree();
                    r.setCode(room.getRoomId());
                    r.setName(room.getRoomName().toString());
                    roomTrees.add(r);
                }
                ajunit.setChildren(roomTrees);
                //把每个单元封装放到unit集合中
                unitTree.add(ajunit);
            }
            //封装的buildingTree对象把unit集合放进去
            buildingTree.setChildren(unitTree);
            //封装的buildingTree对象放入building集合
            buildingtrees.add(buildingTree);
        }
    }
}
